package com.xiang.config;

import com.xiang.filter.RepeatableReadRequestFilter;
import com.xiang.interceptor.RepeatSubmitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

  @Autowired
  RepeatSubmitInterceptor repeatSubmitInterceptor;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
  }

  @Bean
  FilterRegistrationBean<RepeatableReadRequestFilter> repeatableReadRequestFilterFilterRegistrationBean() {
    FilterRegistrationBean<RepeatableReadRequestFilter> bean = new FilterRegistrationBean<>();
    bean.setFilter(new RepeatableReadRequestFilter());
    bean.addUrlPatterns("/*");
    return bean;
  }
}
